package gui;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.*;

import data.User;

/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * Class is a ViewPanel containing JComponents asking for event information.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */

public class EditProfile extends ViewPanel
{
   
    private JTextField firstNameField;
    private JTextField middleNameField;
    private JTextField lastNameField;
    private JTextField emailField;

    /**
     * Construct ViewPanel containing JComponents asking for profile information
     * @param viewManager the container of this panel
     * @param identification specific identification used to reference this panel
     */
    public EditProfile(final ViewManager viewManager, String identification)
    {
        super(viewManager, identification);
        
        Dimension labelSize = new Dimension(70, 20);
        int labelX = 60;
        int labelY = 80;
        int labelSpread = 30;
        
        // labels
        JLabel title = new JLabel("Edit Profile");
        title.setFont(new Font("Serif", Font.PLAIN, 24));
        title.setSize(150, 30);
        title.setLocation(60, 0);
        this.add(title);
        
        JLabel firstNameLabel = new JLabel("First Name:");
        firstNameLabel.setSize(labelSize);
        firstNameLabel.setLocation(labelX,labelY);
        labelY += labelSpread;
        this.add(firstNameLabel);
    
//        JLabel middleNameLabel = new JLabel("Middle:");
//        middleNameLabel.setSize(labelSize);
//        middleNameLabel.setLocation(labelX, labelY);
//        labelY += labelSpread;
//        this.add(middleNameLabel);
        
        JLabel lastNameLabel = new JLabel("Last Name:");
        lastNameLabel.setSize(labelSize);
        lastNameLabel.setLocation(labelX, labelY);
        labelY += labelSpread;
        this.add(lastNameLabel);
        
        JLabel emailLabel = new JLabel("Email:");
        emailLabel.setSize(labelSize);
        emailLabel.setLocation(labelX, labelY);
        labelY += labelSpread;
        this.add(emailLabel);
        
        // text fields
        Dimension textFieldSize = new Dimension(150,20);
        int textFieldX = labelX + (int)labelSize.getWidth() + 20;
        int textFieldY = 80;
        // use same spread as labels
        
        firstNameField = new JTextField();
        firstNameField.setSize(textFieldSize);
        firstNameField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(firstNameField);
        
//        middleNameField = new JTextField();
//        middleNameField.setSize(textFieldSize);
//        middleNameField.setLocation(textFieldX, textFieldY);
//        textFieldY += labelSpread;
//        this.add(middleNameField);
        
        lastNameField = new JTextField();
        lastNameField.setSize(textFieldSize);
        lastNameField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(lastNameField);
        
        emailField = new JTextField();
        emailField.setSize(textFieldSize);
        emailField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(emailField);
        
        // buttons
        JButton saveChangesButton = new JButton("Save Changes");
        saveChangesButton.setSize(150,20);
        saveChangesButton.setLocation(75,300);
        saveChangesButton.addActionListener(new ActionListener()
           {
               @Override
               public void actionPerformed(ActionEvent e)
               {
                  // TODO Auto-generated method stub
                  String newFirstName = firstNameField.getText();
                  String newLastName = lastNameField.getText();
                  String newEmail = emailField.getText();
                  
                  String newName = newFirstName + " " + newLastName;
                  if(!newFirstName.isEmpty() && !newLastName.isEmpty())
                     viewManager.getGs().getUser().setRealName(newName);
                  
                  if(!newEmail.isEmpty())
                     viewManager.getGs().getUser().setEmail(newEmail);
                  
                  System.out.println("new email = " + newEmail);
                  
                  try
                  {
                     viewManager.getGs().getMySql().updateUser(viewManager.getGs().getUser());
                  } catch (SQLException e1)
                  {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();
                  }
                  
                  CalendarPanel cal = (CalendarPanel)viewManager.getView("calendar");
                  JTabbedPane tab = cal.getTabPanel();
                  User u = viewManager.getGs().getUser();
                  int index = viewManager.getGs().getGroup().getUsers().indexOf(u) + 1;
                  tab.setTitleAt(index, u.getRealName());
                  
                  JOptionPane.showMessageDialog(null, "Profile Update!");
               }
              
           });
        this.add(saveChangesButton);
        
        JButton changePwdButton = new JButton("Change Password");
        changePwdButton.setSize(150,20);
        changePwdButton.setLocation(saveChangesButton.getX() + saveChangesButton.getWidth() + 10, 300);
        changePwdButton.addActionListener(viewManager.addLink("change password"));
        this.add(changePwdButton);        
    }

    /**
     * Clears all text fields and check boxes
     */
    public void clearView()
    {
        firstNameField.setText("");
//        middleNameField.setText("");
        lastNameField.setText("");
        emailField.setText("");
    }
    
    public void setView(User u)
    {
       String name = u.getRealName();
       String firstName = name.substring(0, name.lastIndexOf(" "));
       String lastName = name.substring(name.lastIndexOf(" ")+1);
       String email = u.getEmail();

       firstNameField.setText(firstName);
       lastNameField.setText(lastName);
       emailField.setText(email);
    }
}

